package com.data.collect.tool;

import android.os.Environment;

import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;

/**
 * Created by wanggenlin on 17-6-8.
 */
public class FileTool {

    static FileTool instance;
    static final  String fileDir="sdkData";
    public static FileTool getInstance() {
        if (instance ==null){
            instance  = new FileTool();
        }
        makeDirs();
        return instance;
    }

    private static String sdCachPath(){
        return Environment.getExternalStorageDirectory()+ File.separator +fileDir;
    }

    private  static void makeDirs(){
        File file  = new File(sdCachPath());
        if (!file.exists()){
            file.mkdirs();
        }
    }


    public  void wirterFile(String fileName,String content){
        PrintWriter pw=null;
        try {
             pw  = new PrintWriter(new FileWriter(new File(sdCachPath(),fileName)),true);
            pw.write(content);
        }catch (Exception e){
            Logger.e(e.getLocalizedMessage(),e);
        }finally {
            if (pw!=null){
                pw.close();
            }
        }
    }
    public  void wirterFile(File filePath,String content){
        PrintWriter pw=null;
        try {
            pw  = new PrintWriter(new FileWriter(filePath,true));
            pw.write(content);
        }catch (Exception e){
            Logger.e(e.getLocalizedMessage(),e);
        }finally {
            if (pw!=null){
                pw.close();
            }
        }
    }

    private  String dayLogName(){
        String fileName =DateTools.dateFormatterOfDateTimeForNow(DateTools.DATE_FORMAT_STYLE_3)+".txt";
        return fileName;
    }
    public  void  writeDayLog(String content){
        try {
            String fileName =dayLogName();
            File f = new File(sdCachPath()+File.separator+fileName);
            if (!f.exists()){
                f.createNewFile();
            }
            wirterFile(f,content);
            Logger.e("filename  "+fileName);
        }catch (Exception e){

            Logger.e(e.getLocalizedMessage());
        }
    }

}
